﻿using AutoMapper;
using BPMN.Application.Contracts.DTO.Request.Material;
using BPMN.Application.Contracts.DTO.Response.Material;
using BPMN.Application.Contracts.DTO.Response;
using BPMN.Application.IService;
using BPMN.Domain.Entity.Material;
using BPMN.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPMN.Application.Service
{
    public class ProductsService : AppServiceBase<Products>, IProductsService
    {
        public readonly IRepositoryBase<Products> _repostoryBase;
        public readonly IMapper _mapper;
        public ProductsService(IRepositoryBase<Products> repostoryBase, IMapper mapper) : base(repostoryBase)
        {
            _repostoryBase = repostoryBase;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="entity">产品信息</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> AddProducts(ProductsRequestDto entity)
        {
            var list = _mapper.Map<Products>(entity);
            await _repostoryBase.Add(list);
            return new ResultResponseDto<int>();
        }
        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="PId">产品id</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> DeleleProducts(int PId)
        {
            var list = (await _repostoryBase.GetAll()).Where(o => o.PFId == PId).ToList();
            if (list.Count > 0)
            {
                return new ResultResponseDto<int>() 
                {
                    Code = DomianShare.ResultCode.Fail,
                    ErrorInfo = "请先删除子节点",
                    Data = 0
                };
            }
            else
            {
                await _repostoryBase.Delete(PId);
                return new ResultResponseDto<int>()
                {
                    Code = DomianShare.ResultCode.Ok,
                    ErrorInfo = "删除成功",
                    Data = 0
                };
            }

        }

        /// <summary>
        /// 获取产品列表
        /// </summary>
        /// <param name="PFId">父id</param>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<ProductsResponseDto>>> GetProducts(int PFId, string? PName, bool PYesNo)
        {
            List<Products> list = (await _repostoryBase.GetAll()).Where(o=>o.IsDel ==true).ToList();
            list = list.Where(o => o.PFId == PFId).ToList();
            
            List<ProductsResponseDto> listDto = list.Select(o => new ProductsResponseDto
            {
                PId = o.PId,
                PFId = o.PFId,
                PName = o.PName,
                PYesNoQiYun = o.PYesNoQiYun,
                PYesNoEnable = o.PYesNoEnable,
                PDate = o.PDate,
                Psort = o.Psort,
                children = GetProducts(o.PId, PName, PYesNo).Result.Data.Count > 0 ? GetProducts(o.PId, PName, PYesNo).Result.Data : null
            }).ToList();
       
            return await Task.FromResult(new ResultResponseDto<List<ProductsResponseDto>>()
            {
                Code = DomianShare.ResultCode.Ok,
                ErrorInfo = "产品列表",
                Data = listDto
            });
        }

        /// <summary>
        /// 反填产品
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<ProductsResponseDto>> GetProductsById(int Id)
        {
            var list = await _repostoryBase.Get(Id);
            return await Task.FromResult(new ResultResponseDto<ProductsResponseDto>()
            {
                Code = DomianShare.ResultCode.Ok,
                ErrorInfo = "产品列表",
                Data = _mapper.Map<ProductsResponseDto>(list)
            });
        }
        /// <summary>
        /// 产品下拉框
        /// </summary>
        /// <param name="PId"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<JiLianProductsDto>>> XLKProducts(int PId)
        {
            var list = _repostoryBase.GetAll().Result.Where(o=>o.PFId == PId).ToList();
            List<JiLianProductsDto> listDto = list.Select(o => new JiLianProductsDto
            {
                value = o.PId,
                label = o.PName,
                children = XLKProducts(o.PId).Result.Data.Count > 0 ? XLKProducts(o.PId).Result.Data : null
            }).ToList();
            return await Task.FromResult(new ResultResponseDto<List<JiLianProductsDto>>()
            {
                Code = DomianShare.ResultCode.Ok,
                ErrorInfo = "产品列表",
                Data = listDto
            });
        }

        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> UpdateProducts(ProductsRequestDto entity)
        {
            var list = _mapper.Map<Products>(entity);
            _repostoryBase.Update(list);
            return await Task.FromResult(new ResultResponseDto<int>());
        }
    }
}
